//! 互联网相关生成器测试
//! 
//! 测试 src/generators/internet.rs 中的互联网数据生成功能

use dataforge::generators::internet::*;

#[test]
fn test_email_generation() {
    for _ in 0..10 {
        let email = email();
        assert!(!email.is_empty());
        assert!(email.contains('@'));
        assert!(email.contains('.'));
        
        // 验证邮箱格式
        let parts: Vec<&str> = email.split('@').collect();
        assert_eq!(parts.len(), 2);
        assert!(!parts[0].is_empty()); // 用户名部分
        assert!(!parts[1].is_empty()); // 域名部分
        assert!(parts[1].contains('.')); // 域名应该包含点
        
        println!("邮箱: {}", email);
    }
}

#[test]
fn test_username_generation() {
    for _ in 0..10 {
        let username = username();
        assert!(!username.is_empty());
        assert!(username.len() >= 3); // 用户名至少3个字符
        assert!(username.len() <= 20); // 用户名不超过20个字符
        
        // 用户名应该只包含字母、数字和下划线
        assert!(username.chars().all(|c| c.is_alphanumeric() || c == '_'));
        
        println!("用户名: {}", username);
    }
}

#[test]
fn test_domain_generation() {
    for _ in 0..10 {
        let domain = domain();
        assert!(!domain.is_empty());
        assert!(domain.contains('.'));
        
        // 域名不应该以点开头或结尾
        assert!(!domain.starts_with('.'));
        assert!(!domain.ends_with('.'));
        
        println!("域名: {}", domain);
    }
}

#[test]
fn test_url_generation() {
    for _ in 0..10 {
        let url = url();
        assert!(!url.is_empty());
        assert!(url.starts_with("http://") || url.starts_with("https://"));
        
        println!("URL: {}", url);
    }
}

#[test]
fn test_ipv4_generation() {
    for _ in 0..10 {
        let ip = ipv4();
        assert!(!ip.is_empty());
        
        // 验证IPv4格式
        let parts: Vec<&str> = ip.split('.').collect();
        assert_eq!(parts.len(), 4);
        
        for part in parts {
            let num: u8 = part.parse().expect("Invalid IPv4 part");
            assert!(num <= 255);
        }
        
        println!("IPv4: {}", ip);
    }
}

#[test]
fn test_ipv6_generation() {
    for _ in 0..10 {
        let ip = ipv6();
        assert!(!ip.is_empty());
        assert!(ip.contains(':'));
        
        // IPv6应该包含冒号分隔符
        let parts: Vec<&str> = ip.split(':').collect();
        assert!(parts.len() >= 3); // 至少应该有几个部分
        
        println!("IPv6: {}", ip);
    }
}

#[test]
fn test_mac_address_generation() {
    for _ in 0..10 {
        let mac = mac_address();
        assert!(!mac.is_empty());
        
        // MAC地址格式验证
        let parts: Vec<&str> = mac.split(':').collect();
        assert_eq!(parts.len(), 6);
        
        for part in parts {
            assert_eq!(part.len(), 2);
            assert!(part.chars().all(|c| c.is_ascii_hexdigit()));
        }
        
        println!("MAC地址: {}", mac);
    }
}

#[test]
fn test_user_agent_generation() {
    for _ in 0..5 {
        let ua = user_agent();
        assert!(!ua.is_empty());
        assert!(ua.len() > 10); // User-Agent应该比较长
        
        // 应该包含常见的浏览器标识
        assert!(ua.contains("Mozilla") || ua.contains("Chrome") || 
                ua.contains("Safari") || ua.contains("Firefox"));
        
        println!("User-Agent: {}", ua);
    }
}

#[test]
fn test_password_generation() {
    for _ in 0..10 {
        let password = password(12);
        assert!(!password.is_empty());
        assert!(password.len() >= 8); // 密码至少8位
        assert!(password.len() <= 32); // 密码不超过32位
        
        // 密码应该包含多种字符类型
        let has_upper = password.chars().any(|c| c.is_ascii_uppercase());
        let has_lower = password.chars().any(|c| c.is_ascii_lowercase());
        let has_digit = password.chars().any(|c| c.is_ascii_digit());
        
        assert!(has_upper || has_lower || has_digit); // 至少包含一种类型
        
        println!("密码: {}", password);
    }
}

#[test]
fn test_email_uniqueness() {
    let mut emails = std::collections::HashSet::new();
    
    for _ in 0..100 {
        let email = email();
        emails.insert(email);
    }
    
    // 邮箱重复率应该很低
    assert!(emails.len() >= 95);
}

#[test]
fn test_internet_data_format_consistency() {
    for _ in 0..20 {
        let email = email();
        let username = username();
        let domain = domain();
        let url = url();
        let ipv4 = ipv4();
        
        // 格式一致性检查
        assert!(email.matches('@').count() == 1);
        assert!(!username.contains('@'));
        assert!(domain.matches('.').count() >= 1);
        assert!(url.starts_with("http"));
        assert!(ipv4.matches('.').count() == 3);
    }
}